var geocoder;
var map;
var marker;
function initialize() {

	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(-33.455273, -70.587851);
	var mapOptions = {
		zoom : 14,
		center : latlng,
		rotateControl : true
	};
	map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
function codeAddress() {

	var address = document.getElementById('direccion').value;
	address = address + ", " + $("#comuna option:selected").text() + ", chile ";
	geocoder.geocode({
		'address' : address
	}, function(results, status) {

		if (status == google.maps.GeocoderStatus.OK) {

			map.setCenter(results[0].geometry.location);
			if (marker) {
				marker.setPosition(results[0].geometry.location);
			} else {
				marker = new google.maps.Marker({

					map : map,
					position : results[0].geometry.location,
					draggable : true

				});
			}
			updatePosition(results[0].geometry.location);
			google.maps.event.addListener(marker, 'dragend', function() {
				updatePosition(marker.getPosition());
			});
			$(".ok").prop({
				'disabled' : false
			});
		} else {
			alert("No se pudo encontrar la dirección seleccionada");
			$(".ok").prop({
				'disabled' : true
			});
		}
	}

	);
}

function updatePosition(latLng) {

	$('#latitud').val(latLng.lat());
	$('#longitud').val(latLng.lng());

}
google.maps.event.addDomListener(window, 'load', initialize);